package com.hqyj.javaSpringBoot.modules.account.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.account.entity.User;
import com.hqyj.javaSpringBoot.modules.account.service.UserService;
import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;

/**
 * @Description：UserController
 * @Author ：Cwd
 * @Date ：2021/8/26 13:15
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1:8083:8083/api/login ---- post
     * {"userName":"admin", "password":"123456"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    public Result<User> login(@RequestBody User user) {
        return userService.login(user);
    }

    /**
     * 127.0.0.1:8083/api/user ---- post
     * {"userName":"cwd", "password":"939041405"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /**
     * 127.0.0.1:8083/api/user ---- put
     * {"id":"1", "userName":"admin", "password":"123456"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    public Result<User> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /**
     * 127.0.0.1:8083/api/user/1 ---- get
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }

    /**
     * 127.0.0.1:8083/api/user/1 ---- delete
     */
    @DeleteMapping("/user/{id}")
	@RequiresPermissions("/api/user/{id}") //deleteUser
    public Result<Object> deleteUserById(@PathVariable int id) {
        return userService.deleteUserById(id);
    }

    /**
     * 127.0.0.1:8083/api/users ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    public PageInfo<User> getUsersBySearchBean(@RequestBody SearchBean searchBean) {
        return userService.getUsersBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1:8083/api/users/excel?keyWord=hj ---- get
     */
    @GetMapping(value = "/users/excel")
    public void exportExcel(@RequestParam String keyWord,
                            HttpServletResponse response) throws IOException {
        List<User> users = userService.getUsersByKeyWord(keyWord);
    }
}
